/**
 * Dear programmer:
 * When I wrote this code, only god and I knew how it worked.
 * Now, only god knows it!
 *
 * @author: Pegasus  Date: 2018/11/21 Time: 10:06
 *
 * 个人中心
 */

import React, {Component} from 'react';
import {
    Linking,
    SafeAreaView,
    ScrollView,
    TouchableOpacity,
    View,
    NativeModules,
    TouchableHighlight,
    Text,
    Platform,
    BackHandler,
    ToastAndroid,
} from 'react-native';
import {connect} from 'react-redux';
import OffWorkView from "../../../component/OffWorkView/OffWorkView"
import IconsAndTextCellView from "../../../component/IconsAndTextCellView/IconsAndTextCellView";
import DividingLineViewComponent from "../../../component/DividingLineViewComponent/DividingLineViewComponent";
import HS_TouchableHighlight from "../../../hsstyle/HS_TouchableHighlight";
import {styles} from './MineScreen.Style';
import TestScreen from './TestScreen'
import {FONT_COLOR_TITLE} from "../../../constant/PHColors";

class MineScreen extends Component {

  static navigationOptions = ({navigation}) => ({
    headerTitle: ``,
    headerLeft: <View><Text style={{marginLeft:15,fontSize:18,color:FONT_COLOR_TITLE}}>个人中心</Text></View>,
    headerRight: <OffWorkView navigation={navigation}/>
  });

  // 构造
  constructor(props) {
    super(props);
    // 初始状态
    this.state = {};
  }

    componentWillMount() {
        if (Platform.OS === 'android'||Platform.OS==='ios') {
            BackHandler.addEventListener('hardwareBackPress', this.onBackAndroid);
        }
    };

    componentWillUnmount() {
        if (Platform.OS === 'android'||Platform.OS==='ios') {
            BackHandler.removeEventListener('hardwareBackPress', this.onBackAndroid);
        }
    };

    onBackAndroid = () =>{
        if(this.props.navigation.isFocused()){//判断   该页面是否处于聚焦状态
            if (this.lastBackPressed && this.lastBackPressed + 2000 >= Date.now()) {
                BackHandler.exitApp();//直接退出APP
            }else{
                this.lastBackPressed = Date.now();
                ToastAndroid.show('再按一次退出应用', 1000);//提示
                return true;
            }
        }
    };

  render() {
    return (
        <SafeAreaView style={styles.body}>

            <ScrollView style={styles.body}>

                <HS_TouchableHighlight underlayColor={'#f1f1f1'} onPress={() => {
                    this.props.navigation.navigate('AccountSettingsScreen')
                }}>
                    <IconsAndTextCellView title={'账户设置'}/>
                </HS_TouchableHighlight>

                <DividingLineViewComponent lineHeight={1}/>

                <HS_TouchableHighlight underlayColor={'#f1f1f1'} onPress={() => {
                    this.props.navigation.navigate('NewMessageSettingsScreen')
                }}>
                    <IconsAndTextCellView title={'新消息提醒'}/>
                </HS_TouchableHighlight>

                <DividingLineViewComponent lineHeight={1}/>

                <HS_TouchableHighlight underlayColor={'#f1f1f1'} onPress={() => {
                    this.props.navigation.navigate('LocalPhoneNumVerify')
                }}>
                    <IconsAndTextCellView title={'验证本机号码'}/>
                </HS_TouchableHighlight>

                <DividingLineViewComponent lineHeight={1}/>

                <HS_TouchableHighlight underlayColor={'#f1f1f1'} onPress={() => {
                    let Platform = require('Platform');
                    if (Platform.OS === 'android') {
                        NativeModules.OpenSettings.openManageApplicationSettings((data) => {
                            console.log('call back data', data);
                        });
                    }else{
                        Linking.openURL('app-settings:').catch(err => console.log('error', err))
                    }

                }}>
                    <IconsAndTextCellView title={'开启应用保护'}/>
                </HS_TouchableHighlight>

                <DividingLineViewComponent lineHeight={1}/>

                <HS_TouchableHighlight underlayColor={'#f1f1f1'} onPress={() => {
                }}>
                    <IconsAndTextCellView title={'检查更新'}/>
                </HS_TouchableHighlight>

                <DividingLineViewComponent lineHeight={1}/>

                <HS_TouchableHighlight underlayColor={'#f1f1f1'} onPress={() => {
                    this.props.navigation.navigate('TestScreen')
                }}>
                    <IconsAndTextCellView title={'测试界面'}/>
                </HS_TouchableHighlight>

            </ScrollView>

        </SafeAreaView>
    )
  }
}

const props = state => {
  return {}
};

export default MineScreen = connect(props)(MineScreen);